// 计算供房贷款
class LoanUtil {
  constructor(city, that, year, loanTotal) {
    switch (city) {
      case "sz":
        return new SzLoan(that, year, loanTotal);
        break;
    }
  }
}

class SzLoan {
  constructor(that, year, loanTotal) {
    // 总期数
    this.totalTimes = year * 12;
    // 年利率
    this.yearInterestRate = year < 5 ? 0.0275 : 0.0325;
    // 月利率
    this.monthInterestRate = this.yearInterestRate / 12;
    this.that = that;
    this.loanTotal = loanTotal;
    this.that.setData({
      yearInterestRate: this.yearInterestRate * 100,
    });
  }
  /**
   * 等额本息
   */
  equivalentPrincipalAndInterest() {
    // 月供
    let monthMoney = parseFloat(((this.loanTotal * this.monthInterestRate * ((1 + this.monthInterestRate) ** this.totalTimes)) / (((1 + this.monthInterestRate) ** this.totalTimes) - 1)).toFixed(2));
    // 第一期利息
    let firstInterest = parseFloat((this.loanTotal * this.monthInterestRate).toFixed(2));
    // 第一期的本金
    let firstPrincipal = parseFloat((monthMoney - firstInterest).toFixed(2));
    this.that.setData({
      detailList: [{ monthMoney, principal: firstPrincipal, interest: firstInterest }],
      firstMonth: monthMoney,
    });
    for (let i = 1; i < this.totalTimes; i++) {
      let interest = parseFloat(((this.loanTotal - this.totalPrincipal()) * this.monthInterestRate).toFixed(2));
      let principal = parseFloat((monthMoney - interest).toFixed(2));
      this.that.data.detailList.push({ monthMoney, principal, interest, });
    }
    this.that.data.detailList.forEach(v => {
      v.interest = this.fixedData(v.interest.toString());
      v.principal = this.fixedData(v.principal.toString());
    });
    let totalRepayment = monthMoney * this.totalTimes;
    let totalInterest = totalRepayment - this.loanTotal;
    this.that.setData({
      detailList: this.that.data.detailList,
      monthMoney,
      totalRepayment: totalRepayment.toFixed(2),
      totalInterest: totalInterest.toFixed(2),
    });
  }
  // 已还本金总额
  totalPrincipal() {
    let totalPrincipal = 0;
    this.that.data.detailList.forEach(v => {
      if (v.principal)
        totalPrincipal += v.principal;
    });
    return totalPrincipal;
  }

  /**
   * 等额本金
   * 本金=(总本金/总期数)
   * 月供=(本金/还款月数)+(本金-累计已还本金)*月利率
   * 利息=(本金-累计已还本金)*月利率
   */

  equivalentPrincipal() {
    // 第一期的本金
    let firstPrincipal = parseFloat((this.loanTotal / this.totalTimes).toFixed(2));
    // 月供
    let monthMoney = parseFloat(((this.loanTotal / this.totalTimes) + (this.loanTotal * this.monthInterestRate)).toFixed(2));
    // 第一期利息
    let firstInterest = parseFloat((this.loanTotal * this.monthInterestRate).toFixed(2));
    this.that.setData({
      detailList: [{ monthMoney, principal: firstPrincipal, interest: firstInterest }],
      firstMonth: monthMoney.toFixed(2),
    });

    for (let i = 1; i < this.totalTimes; i++) {
      let interest = parseFloat(((this.loanTotal - this.totalPrincipal()) * this.monthInterestRate).toFixed(2));
      let monthMoney = parseFloat(((this.loanTotal / this.totalTimes) + ((this.loanTotal - this.totalPrincipal()) * this.monthInterestRate)).toFixed(2));
      this.that.data.detailList.push({ monthMoney, principal: firstPrincipal, interest, });
    }
    let totalInterest = (this.totalTimes + 1) * this.loanTotal * this.monthInterestRate / 2;
    let totalRepayment = totalInterest + this.loanTotal;
    this.that.data.detailList.forEach(v => {
      v.monthMoney = this.fixedData(v.monthMoney.toString());
      v.interest = this.fixedData(v.interest.toString());
      v.principal = this.fixedData(v.principal.toString());
    });
    this.that.setData({
      detailList: this.that.data.detailList,
      monthMoney,
      totalRepayment: totalRepayment.toFixed(2),
      totalInterest: totalInterest.toFixed(2),
    });
  }
  // 数据格式化
  fixedData(data) {
    let decimal = data.split(".");
    if (decimal.length > 0) {
      if (!decimal[1])
        return data + '.00';
      else if (decimal[1].length === 1)
        return data + '0';
      else
        return data;
    } else {
      return data;
    }

  }
}
module.exports.LoanUtil = LoanUtil;